<?php

namespace app\admin\controller;

use app\admin\model\Fake as ModelFake;
use app\admin\model\GoodsCategory as ModelGoodsCategory;
use think\exception\ValidateException;
use think\facade\Db;

class GoodsCategory extends Base
{
    public function index()
    {
        $list = Db::name('goodsCategory')->select();
        return view('index',['list' => $list]);
    }
    public function add()
    {
        $param = input();
        $res = ModelGoodsCategory::create($param);
        if($res) return success();
        return error();
    }
    public function getFake()
    {
        $param = input();
        $res = ModelFake::getFake($param);
        return success($res);
    }
    public function list()
    {
        $param = input();
        $fake = @$param['fake'];
        if (empty($fake)) {
            $list = ModelGoodsCategory::order('status asc,cid desc')->paginate($param['limit']);
        } else {
            $list = ModelGoodsCategory::where('fake', 'like', '%' . $fake . '%')->order('status asc,id desc')->paginate($param);
        }
        return success($list, '操作成功', 0);
    }
    public function doUpdate()
    {
        if (request()->isPost()) {
            $param = input();
            $res = ModelGoodsCategory::update($param);
            if ($res) return success();
            else return error();
        }
    }

    public function del($cid)
    {
        $res = ModelGoodsCategory::with('goods')->find($cid);
        $res = $res->together(['goods'])->delete();
        if ($res) return success();
        else return error();
    }
}
